package com.supercube.sport.yoga.controller;

import com.github.pagehelper.PageInfo;
import com.supercube.sport.yoga.domain.CourseBasicInfoDTO;
import com.supercube.sport.yoga.service.CourseBasicInfoService;
import com.supercube.sport.yoga.utils.ResultInfo;
import com.supercube.sport.yoga.vo.CourseBasicInfoVO;
import com.supercube.sport.yoga.vo.CourseSuportCard;
import com.supercube.sport.yoga.vo.SuportCardVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Description：
 * Created with IntelliJ IDEA
 * Created By guowen
 * Date: 2020/3/21
 * Time: 17:10
 */
@RestController
@RequestMapping("/courseBasic")
@Api(tags = "课程基本信息")
public class CourseBasicController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CourseBasicInfoService courseBasicInfoService;

    /**
     * 保存课程基本信息
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存课程基本信息", notes = "保存课程基本信息")
    @PostMapping("/save")
    public ResultInfo saveCourseBasic(@RequestBody CourseBasicInfoDTO dto) {
        try {
            String id = courseBasicInfoService.save(dto);
            return ResultInfo.success(id, "保存成功！");
        } catch (Exception e) {
            return ResultInfo.failed("保存失败，清刷新重试！");
        }
    }

    /**
     * 修改课程基本信息
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改课程基本信息", notes = "修改课程基本信息")
    @PostMapping("/update")
    public ResultInfo updateCourseBasic(@RequestBody CourseBasicInfoDTO dto) {
        courseBasicInfoService.update(dto);
        return ResultInfo.success("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除课程基本信息", notes = "删除课程基本信息")
    @GetMapping("/delete")
    public ResultInfo deleteCourseBasic(@RequestParam("id") String id) {
        courseBasicInfoService.deleteCourseBasic(id);
        return ResultInfo.success("删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除课程基本信息", notes = "批量删除课程基本信息")
    @GetMapping("/deleteBatch")
    public ResultInfo deleteBatchCourseBasic(@RequestParam("ids[]") List<String> ids) {
        if (ids == null || ids.size() == 0) {
            return ResultInfo.failed("缺少参数ids");
        }
        ids.forEach(id -> {
            courseBasicInfoService.deleteCourseBasic(id);
        });
        return ResultInfo.success("删除成功");
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @GetMapping("/courseBasicInfo")
    public ResultInfo courseBasicInfo(@RequestParam("id") String id) {
        CourseBasicInfoVO vo = courseBasicInfoService.courseBasicInfo(id);
        return ResultInfo.success(vo);
    }

    /**
     * 查询课程基本信息列表
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询课程基本信息列表", notes = "查询课程基本信息列表")
    @PostMapping("/list")
    public ResultInfo selectCourseBasicList(@RequestBody CourseBasicInfoDTO dto) {
        List<CourseBasicInfoVO> list = courseBasicInfoService.selectCourseBasicList(dto);
        PageInfo<CourseBasicInfoVO> pageInfo = new PageInfo<>(list);
        return ResultInfo.success(pageInfo);
    }

    /**
     * 查询所有课程基本信息列表
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询所有课程基本信息列表", notes = "查询所有课程基本信息列表")
    @PostMapping("/selectAll")
    public ResultInfo selectAll(String classType) {
        List<CourseBasicInfoVO> list = courseBasicInfoService.selectAll(classType);
        return ResultInfo.success(list);
    }

    /**
     * 查询课程支持的会员卡
     *
     * @return
     */
    @ApiOperation(value = "查询课程支持的会员卡", notes = "查询课程支持的会员卡")
    @GetMapping("/queryCoursesSupportedCard")
    public ResultInfo queryCoursesSupportedCard(@RequestParam("courseId") String courseId) {
        List<SuportCardVo> list = courseBasicInfoService.queryCoursesSupportedCard(courseId);
        return ResultInfo.success(list);
    }


    /**
     * 课程关联会员卡
     *
     * @return
     */
    @ApiOperation(value = "保存课程关联会员卡", notes = "保存课程关联会员卡")
    @PostMapping("/saveCoursesSupportedCard")
    public ResultInfo saveCoursesSupportedCard(@RequestBody CourseSuportCard courseSuportCard) {
        courseBasicInfoService.saveCoursesSupportedCard(courseSuportCard);
        return ResultInfo.success("保存成功");
    }
}
